package com.hengpeng.itfints.pagination.impl;

import java.util.Collection;

import com.hengpeng.itfints.pagination.Pagination;

/**
 * <pre>
 * 分页精简结果实体
 * </pre>
 * @author chenjian
 * @version 1.0, 2013-8-21
 */
public class PaginationImpl<T> extends PageImpl implements Pagination<T>
{
	/**
	 * 总共记录数
	 */
	private int totalCount;

	/**
	 * 当前页数据集
	 */
	private Collection<T> result;

	/**
	 * 页面显示工具栏个数，默认显示7个
	 */
	private int pageBarCount = 7;

	/**
	 * 分页条开始页数
	 */
	private int beginPageBarNum = 0;

	/**
	 * 分页条结束页数
	 */
	private int endPageBarNum = 0;

	public PaginationImpl(int currentPage, int pageSize, int totalCount, Collection<T> result)
	{
		super(currentPage, pageSize);
		this.totalCount = totalCount;
		this.result = result;
		init();
	}

	private void init()
	{
		if (getTotalPageRows() <= pageBarCount)
		{
			beginPageBarNum = 1;
			endPageBarNum = getTotalPageRows();
		}
		else
		{
			int _nQian = pageBarCount / 2;// 当前页按钮前面显示的按钮个数
			int _nHou = pageBarCount - _nQian - 1;// 当前页按钮后面显示的按钮个数
			while (getCurrentPage() <= _nQian)
			{
				_nQian -= 1;
			}
			while (getCurrentPage() + _nHou > getTotalPageRows())
			{
				_nQian += 1;
				_nHou -= 1;
			}
			beginPageBarNum = getCurrentPage() - _nQian;
			endPageBarNum = beginPageBarNum + pageBarCount - 1;
		}
	}

	public int getTotalCount()
	{
		return totalCount;
	}

	public void setTotalCount(int totalCount)
	{
		this.totalCount = totalCount;
	}

	public Collection<T> getResult()
	{
		return result;
	}

	public void setResult(Collection<T> result)
	{
		this.result = result;
	}

	public int getPageBarCount()
	{
		return pageBarCount;
	}

	public void setPageBarCount(int pageBarCount)
	{
		this.pageBarCount = pageBarCount;
	}

	public int getBeginPageBarNum()
	{
		return beginPageBarNum;
	}

	public int getEndPageBarNum()
	{
		return endPageBarNum;
	}

	/**
	 * 获取总页数
	 */
	public int getTotalPageRows()
	{
		return totalCount % getPageSize() == 0 ? totalCount / getPageSize() : totalCount / getPageSize() + 1;
	}

	/**
	 * <pre>
	 * 是否为首页
	 * </pre>
	 * @return
	 */
	public boolean isFirstPage()
	{
		return getCurrentPage() == 1;
	}

	/**
	 * <pre>
	 * 是否为尾页
	 * </pre>
	 * @return
	 */
	public boolean isLastPage()
	{
		return getCurrentPage() == getTotalPageRows();
	}

	/**
	 * <pre>
	 * 下一页
	 * </pre>
	 * @return
	 */
	public int getNextPage()
	{
		return getCurrentPage() >= getTotalPageRows() ? getCurrentPage() : getCurrentPage() + 1;
	}

	/**
	 * <pre>
	 * 上一页
	 * </pre>
	 * @return
	 */
	public int getPrePage()
	{
		return getCurrentPage() <= 1 ? 1 : getCurrentPage() - 1;
	}
}
